class AddEmployeeCertificate < Mutations::Command
  include Wisper::Publisher

  required do
    string  :employee_id
    integer :certificate_id
    date    :valid_from
    string  :number
  end
  
  def execute
    unless employee = Employee.find(employee_id)
      add_error(:employee_id, :employee_not_exist, '员工不存在')
      return
    end

    unless certificate = Certificate.find_by_id(certificate_id)
      add_error(:employee_id, :certificate_not_exist, '证书号不存在')
      return
    end

    employee_certificate = EmployeeCertificate.new inputs
    if employee_certificate.save
      return employee_certificate
    else
      employee_certificate.errors.each do |key, value|
        add_error(key, key, value)
      end
    end
  end
end